package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.voute.Model.Bean.SimpleCircleInfoEntity;
import com.voute.R;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.CircleActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xianke on 22/04/2017.
 */

public class SimpleCircleInfoAdapter extends BaseAdapter {

    private List<SimpleCircleInfoEntity> datas = new ArrayList<>();
    private Context context;

    public SimpleCircleInfoAdapter(List<SimpleCircleInfoEntity> sources, Context c) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            context = c;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_simple_cricle, null);
        }
        initViews(convertView, position);
        return convertView;
    }

    public void setDatas(List<SimpleCircleInfoEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    private void initViews(View v, int position) {
        final SimpleCircleInfoEntity entity = datas.get(position);
        TextView tv_notify = ViewHolderUtils.get(v, R.id.tv_notify);
        TextView tv_circle_title = ViewHolderUtils.get(v, R.id.tv_circle_title);
        tv_circle_title.setText(entity.getCircle_name());
        if (entity.getUnread() > 0) {
            tv_notify.setVisibility(View.VISIBLE);
        } else {
            tv_notify.setVisibility(View.GONE);
        }
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CircleActivity.class);
                intent.putExtra("KEY_CIRCLE_TITLE",entity.getCircle_name());
                intent.putExtra("KEY_CIRCLE_HASH",entity.getHash());
                context.startActivity(intent);
            }
        });
    }
}
